package ve.co.bsc.appdescarga.web;

import javax.validation.Valid;
import org.springframework.roo.addon.web.mvc.controller.RooWebScaffold;
import ve.co.bsc.appdescarga.domain.Configuracion;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.ByteArrayMultipartFileEditor;

@RooWebScaffold(path = "configuracion", automaticallyMaintainView = true, formBackingObject = Configuracion.class)
@RequestMapping("/configuracion/**")
@Controller
public class ConfiguracionController {

    @InitBinder
    public void initBinder(WebDataBinder binder)
    {
        binder.registerCustomEditor(byte[].class, new ByteArrayMultipartFileEditor());
    }

    @RequestMapping(value = "/configuracion", method = RequestMethod.POST)
    public String create(
            @Valid Configuracion configuracion,
            BindingResult result,
            MultipartHttpServletRequest request,
            ModelMap modelMap
            ) {
        if (configuracion == null) throw new IllegalArgumentException("A configuracion is required");
        if (result.hasErrors()) {
            modelMap.addAttribute("configuracion", configuracion);
            return "configuracion/create";
        }
        configuracion.persist();
        return "redirect:/configuracion/" + configuracion.getId();
    }

    @RequestMapping(method = RequestMethod.PUT)
    public String update(
            @Valid Configuracion configuracion,
            BindingResult result,
            MultipartHttpServletRequest request,
            ModelMap modelMap
            ) {
        if (configuracion == null) throw new IllegalArgumentException("A configuracion is required");
        if (result.hasErrors()) {
            modelMap.addAttribute("configuracion", configuracion);
            return "configuracion/update";
        }
        configuracion.merge();
        return "redirect:/configuracion/" + configuracion.getId();
    }

}
